package mercurius.fitness.client.util;

import net.pleso.framework.client.ui.windows.ErrorMessageWindow;

import com.google.gwt.core.client.GWT;

import mercurius.fitness.client.rb.message.Message;
import mercurius.fitness.client.rb.message.MessageDataSet;

public class Logger {

	public final static String DEBUG = "DEBUG";
	public final static String INFO = "INFO";
	public final static String WARN = "WARN";
	public final static String ERROR = "ERROR";
	public final static String FATAL = "FATAL";

	private static int index = 2;
	
	public static void setLoggingLevel(String l) {
		if(l.equalsIgnoreCase(DEBUG) || l.equalsIgnoreCase(INFO) || l.equalsIgnoreCase(WARN) || l.equalsIgnoreCase(ERROR) || l.equalsIgnoreCase(FATAL)) {
		} else {
		}
		index = getLogIndex(l);
	}
	
	private static int getLogIndex(String l) {
		if(l.equalsIgnoreCase(DEBUG)) return 1;
		if(l.equalsIgnoreCase(INFO)) return 2;
		if(l.equalsIgnoreCase(WARN)) return 3;
		if(l.equalsIgnoreCase(ERROR)) return 4;
		if(l.equalsIgnoreCase(FATAL)) return 5;
		return 2;
	}
	
	private static void log(String level,String message, Throwable e) {
		
		if(getLogIndex(level) >= index) {
			
			if(getLogIndex(level) >= getLogIndex(ERROR)) {
				ErrorMessageWindow w = new ErrorMessageWindow();
				 w.showError(message,e);
			}
			
			if(e != null) {
				GWT.log(level+" :"+message,e);
				MessageDataSet.add(new Message(level,message +"\n"+ e.getMessage()));
			} else {
				GWT.log(level+" :"+message,null);
				MessageDataSet.add(new Message(level,message));
			}
		}
	}
	
	public static void debug(String message,Throwable e) {
		log(DEBUG,message,e);
	}
	
	public static void debug(String message) {
		log(DEBUG,message,null);
	}
	
	public static void info(String message,Throwable e) {
		log(INFO,message,e);
	}
	
	public static void info(String message) {
		log(INFO,message,null);
	}
	
	public static void warn(String message,Throwable e) {
		log(WARN,message,e);
	}
	
	public static void warn(String message) {
		log(WARN,message,null);
	}
	
	public static void error(String message,Throwable e) {
		log(ERROR,message,e);
	}
	
	public static void error(String message) {
		log(ERROR,message,null);
	}
	
	public static void fatal(String message,Throwable e) {
		log(FATAL,message,e);
	}
	
	public static void fatal(String message) {
		log(FATAL,message,null);
	}
}
